﻿using System;
using System.Buffers;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Demo.AttachFileService.Tools
{
    class Program
    {
        const uint FH = 0x30316364;
        static void Main(string[] args)
        {
 
            var reader = new ReadOnlySequence<byte>(new byte[] { 0x30, 0x31, 0x63, 0x64 });
            var rr = new SequenceReader<byte>(reader);
            if(rr.TryReadBigEndian(out int ss))
            {
                if(ss == FH)
                {
                    Console.WriteLine("OK");
                }
                else
                {
                    Console.WriteLine("NO");
                }
            }
            
            //var tools = new Tools();
            //tools.Start();

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }

    }

    public static class aa
    {
        /// <summary>
        /// 16进制字符串转16进制数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static byte[] ToHexBytes(this string hexString, string separator = " ")
        {
            hexString = hexString.Replace(separator, "");
            byte[] buf = new byte[hexString.Length / 2];
            ReadOnlySpan<char> readOnlySpan = hexString.AsSpan();
            for (int i = 0; i < hexString.Length; i++)
            {
                if (i % 2 == 0)
                {
                    buf[i / 2] = Convert.ToByte(readOnlySpan.Slice(i, 2).ToString(), 16);
                }
            }
            return buf;
        }

        public static string ToHexString(this byte[] source)
        {
            return HexUtil.DoHexDump(source, 0, source.Length).ToUpper();
        }
    }

    public class Tools
    {
        private Socket socket;
        public byte[] jt9010 = "7E121000A500000212002200013231323030323232313230303232210322174610000200333233313332333033303332333232313033323231373436313030303032303001023130305F36355F363531305F305F33323331333233303330333233323231303332323137343631303030303230302E6A7067000189BE3130335F30305F363531305F305F33323331333233303330333233323231303332323137343631303030303230302E62696E00000C80737E".ToHexBytes();
        public byte[] jt9011 = "7E1211003700000212002200023130305F36355F363531305F305F33323331333233303330333233323231303332323137343631303030303230302E6A706700000189BE647E".ToHexBytes();
        public byte[] jt9012 = "7E1212003700000212002200033130305F36355F363531305F305F33323331333233303330333233323231303332323137343631303030303230302E6A706700000189BE667E".ToHexBytes();
        public byte[] filedata = "3031636430305F36355F363530345F305F33393331333133323337333333363231303332303131303032313030303630302E6A70670000005000000010003DD4F6566B69617614CB0AB318D243FF002D141E80F5AEFE49FE7CB29200E0FE15C478920136A33ACB1900C6A1837D28031A58C5BDF4D6EFB860E79F7AAF2168C48771DBB7760003F23D6A5491E486169C9F3ED5BECD2FA95FE06F7CAFF2A65C001493C8E703AF140183E3982EAD7C44BA7DE4B048B6D6302422139508C9E60CE7BE5CE6B99B4E8BEDC568EB32BBF882F3CC7672BB154B1C90BB1768FC07159D1FCB348A3A06A00BF07DE34F95728E06391496FDCD4920E3BF4A00AB8DF65FEEF4A934F433E9BA9DB9E7740EC3F01BBF98A65B906DE41E953787DBFE263E5F4DEBB7F3E28039CB660635F4C62B35D364847A122AF420C7BA33D50907F0AAB7848948EC4E6801A5847F7797FEF7A7D2A23C9CFEB45250014519A0D00145149F4EB40050067FC6971EBF9568E95A4DDEA6CE2DA3CA460B3C8784418CF27FA75A00CE031D3F335D1E8DE199AE879B7CFF006484AEF50EA7CC9476DA31C671D4D743A26916BA608E58D3ED17B95225703E4393F754F1F9F3F4AD2B82D2B618925B12A3BB639E79FA03DA802AC56F6FA7D898AD922442F82A3963DFE63D4E3F4ACB195BACA80327935B0C18A3613E465E6475180C57391DFAE6B164661759E091DF1401AF0AA1B51126E8E301CBB2AFCAC701406EE7AD584C1757F306FC8427660907A023D891CD36127CB429B99811FC583824E7F01D6A4E048BCEC2CD9231F7891807F4A00F2ADD4F53914CC743DA95475A0075141A4A002B7FC0836F88E2B9ED6914D75F4D91B11FAE2B02B73C36C61D3B5CB81BB8B4100207792451FC95A803164249E7AF7A41437DE340A0075250296800A5A28A005A28A5A002969296800C51450680133814C14A7938A463CF14009D4E69EBCD32A685726802C44302A4069A2A5863F33AF4E9400D07270064FB549E4CA7F8315A105AB9C055E9E9CD5F8ACA63C0F978EFC66803044127A0FCE93C9907F08FCEBA0F298700038F6EB4AC818E1E303FDD18A00E65D64C1DE8420A66F01829E0FA574DF6652BB8038CE39ED4C92D41C165040E064668039EDE14E3233E94F0C0F438AD73610B706341EFB6A27D2E163F2E548E854D006752E6B40E958E04AC7F0150B69973FF002CD95B9E8DC50055A2A49AD2EA100BC59078F9483503164FBE8CB8EB91400EA86660833FA539E555196E3EB54A4732B649E28010932364F4A91476148A3B0A9512802C69B7979A6DC79FA75E5CD9CC463CCB794C6C47D41ADDB7BEBCD6116E357BCBABFB847DAAF73334857E993C573A7A715ABA0B6D2EBC72EA6803B5F08DE25944ED73119ECAE43DBDEC07FE5A44C79FC47041EC40ABB3584BA35CC304D309ED261BAD6E97EECC9FD18742BD8D63680736CEA7FE7A30FD6BAAD27508EDED9B4ED4E15B8D2A57DE549C18DF1C32B7F0B0F51F8E4500356450A327B75C55D8CE429ED525E6832DBC0B77A74E6FF4F2BBF231E6C40FFCF441FF00A12E41F6AAB0CC9E583B860F7A002E6D1B56BCB2D1A36DADA8CEB033671B22EB237E0818D7AADD4169AA86B8B3D3744D5923223B69F49BA36B771060113249049584EEDC49FBBC0AE37C036EE97DA878859350F2EDD469F69258C71BCAB238DF2C8AAFC1C280BD09F9B815DD7870DBF8835C69A49F49D45ACC79E667D35ED6F2DE57E23DCA70388F8CF5CFB50069DD58BC5652DC9BEBAB9874FB7C20BA6F371B4600C8E598E39279351ADEEAC648952D6CA549B698DD6E590107D72BC558D5EE5268CD945222C0B924E08DE7DE9F61B562815B9F2BEE8EE280126D46F2C416BFD235089539F32D556E1476E8A777E955E0F1AF87E1746BCD485B1CE3FD32096200E7BEE5007E75B73CA4C4C36B73C16534FB42CF6A22936CF1E395930EB8FA1A00A47C4905CAABD83C53C0C0ED31386C9F6AA53EAFB02A2C2FF2A939C647BE6AE49E12F0E5DBF9C74C8ACE773FEB6D1DA0607DF6F19FC2A8CFE14BBB4DEDA4EB467238106A481D5BFE068030FA9068033AE3509E48D923CF23E847E3502C4CBF3103CEDA410A324FD4D4EE9AE152B2787E66DA3EF5ACE92464FA82086FC08ACC7D5D2DA6749C5C5AB1182268CA7D3A8A00D2545628BF31CF00F4A99A3DBB818DB677E7BE3D7D2B31B539320C5226063AA034D3AADD0003A44463A16C0C7D68034BCA5F2F7260673C64649FA548D011808A067E99C631590BAD8420CD6ADB5BB2B8C903BFB558B6D6EDAE26F2951C31E36EDC9EBED40166452508603208E01CE39EB589A849B50AE38E7BD6BC9324AD208C800724038000F6EB5CFEA2AC660719CF4C1EA28021443223855272B8EB5CB6AC43DE3C838DE318FD3AD761137FA3CF852FB22720743F53F4CD725A800190649DA3BD0073B77B6DE70EF9314C86270A7A1EA8DF81E3E8689F735AC8C00DC171C55DD4ED7ED3A7CB19C8C83861D8F506AAD923DD462172AB2CACAA4B361412477F4A00E53C68A89E2389A38C224B650B7CBFC4402A4FE62B05B8BA6F700D77FF0014346B9B3B2D1AFEED1239125974F60BFC417122B67A1FBCDC8AE0AE4E2688E3F8719A00BB6E7A54CC78AAB037CAA6AD11C7AFA50054B4FF0096CBE869B613186FE27C6706A4846DBA9948E48AAD2131DCE33D0E68033B514F2757BD8F39025623F1E7FAD675E0E41F7AD8D7D76EAE64E313468FFA63FA564DC8CA1A00AB49452D00252538024F1F89A5F957D09F5ED400D0A48CF4152C10CB3CAB0DBC6EF239C2A20CB31FA56E691E1CBABA68A5BD0F6B6AFF0036F61F3B0FF654FF00335D75A59C3616D25BE9D0888B005A5C6E918E7A13D7F2C50060695E1558C87D5598BF516B11193CF466EDF415D10522048AD61C42A0A88906141C6467DCF4A7AEEDCAFB479795E4F5E07534D11AFCA5B3B8ED0327080073FAF27F4A0058D999A32DF232BC5B9611C8618041CFD73F9D24312ABA459C9DAC11FA951BB2473E84FEB5327985D4B21019B9E79070E33F9EDA8D06C887424A1276F725793F9E28018E04B1485C9DD2001CA0E01462C7F0C822B1AE80FB564719FF0022B7D801950E00040E3AFD7F139FCEB02FF9B88DB9C13401B368BFB8539E4021B3EE38FD6A6ED804EE38CB0AAD678311FC0F06AC739FD79A00F2BFE114ABD4D4B142D22C400FBEC141FCE99B48620F50714005369D486801B5BF66862F065ECA188F3EFA28B03F882A331FD585609AE97515FB3F817448F0035CDCDCDC9C77036C63FF00413401CD77A51494B400EA07340A5A0028A296800A5E28A2800A5A4A5340052138A334D6392050027F5A683934A7938A1B006680140C9C55B8D76AD436EB939FC6ACD002D740B6C6CAD9A192306E19407DDFC1DF1F5ACBD21436A36E59772895723F1CD6B296B8984D2B64C8CC719E7AF2C7EB9FD280356C9521B4DE883774DC4E3AFB531DCEFCB4BBC83D14D3ED446222668F7AA81C77EB4D2F1B82228847EFEB4011924939E33CD2305232091DC8F7A94464360E4F14C0A086C751C6050033CB608CCBC8079A14129CF7ED4EC18C0E7DB1430641BBA81D71400D09C1E298E368070706A7886E63E8071483703B48DC07228019E5E38E946DF9C7A74CD4E39CB6323BD2A20CF3F5A008BC918CE3E6A61B64720B0078EFD8D5C8D73C5298FE518F5E6803367B0F39079B02CB1AF4DC3A1AACFA5DA971B6D2107FDDE2B781CA900E453362F451401CFB787ED0444A89D1B9C307C8CFD08E954E6D1658F1B6604119F99715D70819C850A181ED9A6496EA51948EC701B9C500710F673A3606D6EFC1AB9A3ABC775F38C640C7E75A0D1004F7A62C611D481C83D680377436DAD283FDEC8AE860E400DC835CD69DF2CD9F5AE92D8FF2A00B3637D77A33896C6E088D06E30B13B7D7E523953F4FCABB8D01349D6AF209F5EB21697D236C6759516443CE6391738DFC1EA037B1AE1D9723032A3FBCBC115D7E95A97F6CD8CB71A8CE6E356B5845BEA08F0AB096DD98A2DF0E07EF220E41EBC73400E84F86EF628677D260BD915658D96F21284BF981A2946D384F972081C9F6ABD6F71A95BDBCB73677D769702505ED1B2D6462DB808149CAE307907DEB1F4EB47B19751D2EE4AC8322349C29192B9C11EC47E62A47173613379E329838643956F61401D7586BF04B35BC3AA84D32F27C7960B661949CF08FD09F6383CD747B4C276B30DC064823A571B697B6F776EF6B7D0477D672A28292A87C1CFA1E9566CF4ED434775FF847EF83C04E4595F39787FE0321F9A3E3A0391401D8C172A8D894875272739E956E3447198F2CB8C95CE0D72963AF4125DAD9EA71BE9B7C4FCB1DC10564FF0071C7CADF81ADF8659124DE8F824F4EF401B56D217DA1DC601CED23193DB9A709C6DDACC627230726AB5ADC8639C853D4EEC1E6A69E36931DC1E06075F7A0011F62F971E5635FBCCB48CECC8B0B859108C624191CFF00F5AA0F2A447C44719187CF4351B395216552B96C038CFD4F14015EEB4BD36E7026D3E2DC0104A7C8D8FC3A1AC4B9F0A00CC6C2E8306C1F2EE0804107FBD8E7F215D1B02EFBC659FD7B8351ECDD2E1976B824B0E9401C65EE8D736DBD6784A119DC71C7E62B15D9A09728C703938E95E9324ECAACB8246DE87D7D7DEB12FF004EB7BE504A98A52301A20003EB91DE80386B8D3A7D5AFE5B9BD9C28C61029E00EC00AC5B6B8B8401EDE6E06461812383FE7A5778BE1EB94665B4923B8001C053B580EE706B85D3CED9A58E46F3228DDC0D9C8EB8C8C75A00B8BAAC8D1BC7776CB23329F9E393000FA1FE5546E2586E5C307C63B37069D71342A194021BA056183F5AAC3CA640B9418EF400FD8BE5BE30DC1C2FAD64E8255EEF4F8E4057122ABB188C9D0F24A8EBC0E47A66B5E54B748A59BCDC471465D88E3F0A8BC01757373AC5E25BDB6A72280B32B69A57CD8588312B9CF5C7984FE19ED400CF89D14737C28D2AEA092078E2D71827D9A6DF0FCF0927683F3274E50F426BC86E47C91BFA3609AF66F8E1A95BDB7C35D1ECE48A58EFF54D48DF0F3A148A468514A8670BC6EF980DDC138AF169DB36BEB820D005AB624818ABB54AD0678ABAC3F950055185BEEBD4556BE1B6524E7915665E2EE23C60F1515FAFEF149381D3340157C42B98B4E9801F323213EA41CFF5AC79C7CA6B7358064D02D5F3FEAE723F31FF00D6AC57195FC2802803D29E171CB7E429071C0ADCF095B59DD6B7147A845E74451CAA6EC0670A4807D471D2802B68FA2DEEAEF8B48C2C2A70F33FCB1A7D4F73EC39AED34AD12C748F9942DDDD8FF96F22FCA87FD95EDF53CFD2B424958A22A8091A70B1A0C2A8F4007029A0104955FA50024CE0E5E56663903713DCF03F9D45D600AAC4AF98632E38E9F2939FF39A95D786CE570A724761FE4E697CA707EE6414049231F36720714002AEF692403965E87181C70303A6334DCA29C2AA9427706F7E31FD69E3E7525C8C16C67AE793FAF14A5B3CB600271C8EE0F140098044815B7FCEE577F6EE17F03404519400632460F4A5DBF30DCA082DCE07AFAFE946D230320F183F5ED400A3EEF60481923DAB9FD5062753C6037AD6F9EE7BD606B431DBBE28034EC7E543567B76C552D38FEEC7D3BD5D1FFD7A00F39494C56B68EBCB236E03F3AAD9DCCCC47524D5EB451E5D9120105B91F81AA3FC4DF534005252E2928012BA2F167EEAC3C3F69D0C3A746CDFEF48CD27F2615CE952C081D4F15D27C4120789AE22520A40B1DB8C7FB11AA9FD41A00E681C0A5C9CD274A51D6801D4B452D00145145002D14514005145250004E0533B7D6873CE290D001CE7029ECB93C739A44157C5994B14B9738DEF8543D4AE3A".ToHexBytes();

        public void Start()
        {
            Init();

            var t = new Thread(A);
            t.Name = "A";
            t.IsBackground = true;
            t.Start();

            var t2 = new Thread(B);
            t2.Name = "B";
            t2.IsBackground = true;
            t2.Start();

            Console.WriteLine("OK");
            Console.ReadLine();
        }

        public void A()
        {
            while (true)
            {
                socket.Send(jt9010);
                //Thread.Sleep(200);

                socket.Send(jt9011);
                //Thread.Sleep(200);

                socket.Send(filedata);
                Thread.Sleep(10000);

                socket.Send(jt9012);
                //Thread.Sleep(200);
            }


        }

        public void B()
        {
            while (true)
            {
                byte[] resBytes = new byte[1024];
                int bytes = socket.Receive(resBytes, resBytes.Length, 0);

                Console.WriteLine(resBytes.ToList().Take(bytes).ToArray().ToHexString());
            }
        }

        public void Init()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                //服务器连接成功
                socket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 808)); //配置服务器IP与端口
            }
            catch
            {
                Console.WriteLine("连接服务器失败！");
            }
        }


    }

    public static class HexUtil
    {
        static readonly char[] HexdumpTable = new char[256 * 4];
        static HexUtil()
        {
            char[] digits = "0123456789ABCDEF".ToCharArray();
            for (int i = 0; i < 256; i++)
            {
                HexdumpTable[i << 1] = digits[(int)((uint)i >> 4 & 0x0F)];
                HexdumpTable[(i << 1) + 1] = digits[i & 0x0F];
            }
        }

        public static string DoHexDump(ReadOnlySpan<byte> buffer, int fromIndex, int length)
        {
            if (length == 0)
            {
                return "";
            }
            int endIndex = fromIndex + length;
            var buf = new char[length << 1];
            int srcIdx = fromIndex;
            int dstIdx = 0;
            for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
            {
                Array.Copy(HexdumpTable, buffer[srcIdx] << 1, buf, dstIdx, 2);
            }
            return new string(buf);
        }

        public static string DoHexDump(byte[] array, int fromIndex, int length)
        {
            if (length == 0)
            {
                return "";
            }
            int endIndex = fromIndex + length;
            var buf = new char[length << 1];
            int srcIdx = fromIndex;
            int dstIdx = 0;
            for (; srcIdx < endIndex; srcIdx++, dstIdx += 2)
            {
                Array.Copy(HexdumpTable, (array[srcIdx] & 0xFF) << 1, buf, dstIdx, 2);
            }
            return new string(buf);
        }


   }    
}
